package com.cskaoyan.service;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.*;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsProductMapper  productMapper;
    @Override
    public BaseRespVo getList(Integer page, Integer limit, Integer userId) {
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        footprintExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketFootprint> footprints = footprintMapper.selectByExampleSelective(footprintExample);
        PageHelper.startPage(page,limit);
        List< Map<String,Object> > list=new ArrayList<>();
        for (MarketFootprint footprint : footprints) {
            Map<String,Object> map=new HashMap<>();
            Integer goodsId = footprint.getGoodsId();
            MarketGoods good = goodsMapper.selectByPrimaryKey(goodsId);
            /*MarketGoodsProductExample productExample = new MarketGoodsProductExample();
            productExample.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
            List<MarketGoodsProduct> productList = productMapper.selectByExampleSelective(productExample);
            MarketGoodsProduct product = productList.get(0);*/
            if (good != null) {
                //map.put("addTime",Date.from(footprint.getAddTime().atZone( ZoneId.systemDefault()).toInstant()));
                map.put("addTime",footprint.getAddTime());
                map.put("brief",good.getBrief());
                map.put("goodsId",goodsId);
                map.put("id",footprint.getId());
                map.put("name",good.getName());
                map.put("picUrl",good.getPicUrl());
                map.put("retailPrice",good.getRetailPrice());
                list.add(map);
            }
        }
        PageInfo< Map<String,Object> > pageInfo = new PageInfo<>(list);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.ok(data);
    }

    @Override
    @Transactional
    public BaseRespVo deleteFootprint(Integer id, Integer userId) {
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        footprintExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(userId).andIdEqualTo(id);
        List<MarketFootprint> footprints = footprintMapper.selectByExampleSelective(footprintExample);
        footprints.get(0).setDeleted(true);
        footprintMapper.updateByPrimaryKey(footprints.get(0));
        return BaseRespVo.ok();
    }
}
